package service

import "tutorial-go/model"

type CustomerService struct {
	customers   []*model.Customer
	customerNum int
}

func NewCustomerService() *CustomerService {
	var customerService = &CustomerService{}
	customerService.customerNum = 1
	var customer = model.NewCustomer(1, "张三", "男", 10, "999", "zs@sohu.com")
	customerService.customers = append(customerService.customers, customer)
	return customerService
}

func (s *CustomerService) List() []*model.Customer {
	return s.customers
}

func (s *CustomerService) Add(customer *model.Customer) bool {
	s.customerNum++
	customer.Id = s.customerNum
	s.customers = append(s.customers, customer)
	return true
}

func (s *CustomerService) Delete(id int) bool {
	var index = s.FindById(id)
	if index == -1 {
		return false
	}
	s.customers = append(s.customers[:index], s.customers[index+1:]...)
	s.customerNum--
	return true
}

func (s *CustomerService) FindById(id int) int {
	var index = -1
	for i := 0; i < s.customerNum; i++ {
		if s.customers[i].Id == id {
			index = i
			break
		}
	}
	return index
}
